package com.jxmcloud.business.cut.logic;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.jxmcloud.business.cut.json.DataConfigAwardLoader;
import com.jxmcloud.business.cut.vo.json.ConfigAwardVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Component
public class DataConfigActionLoader {
    @Resource
    private DataConfigAwardLoader dataConfigAwardLoader;

    // 转盘配置
    private HashMap<Integer, LinkedList<ConfigAwardVo>> awardPoolMap = new HashMap();


    // 配置比表二次加载
    public boolean initAllConfig() {
        this.loadConfigAward();

        return true;
    }

    //
    private boolean loadConfigAward() {
        this.awardPoolMap.clear();
        Map<Integer, ConfigAwardVo> configAwardMap = dataConfigAwardLoader.getConfigAwardMap();
        List<ConfigAwardVo> validAwards = configAwardMap.values().stream().collect(Collectors.toList());
        for (ConfigAwardVo validAward : validAwards) {
            Integer poolId = validAward.getPoolId();
            List<ConfigAwardVo> configList = this.awardPoolMap.get(poolId);
            if (ObjectUtil.isNull(configList)) {
                LinkedList<ConfigAwardVo> newList = Lists.newLinkedList();
                newList.add(validAward);
                this.awardPoolMap.put(poolId, newList);
            } else {
                configList.add(validAward);
            }
        }

        // 排序
        for (Map.Entry<Integer, LinkedList<ConfigAwardVo>> entry : this.awardPoolMap.entrySet()) {
            Integer key = entry.getKey();
            LinkedList<ConfigAwardVo> value = entry.getValue();
            LinkedList<ConfigAwardVo> sortTemo = this.sortList(value);
            if (ObjectUtil.isNotNull(sortTemo)) {
                this.awardPoolMap.put(key, sortTemo);
            }
        }

        return true;
    }

    /**
     * 排序装盘 按条件大小排序
     *
     * @param data 转盘
     * @return 有序转盘
     */
    private LinkedList<ConfigAwardVo> sortList(LinkedList<ConfigAwardVo> data) {
        // 使用 TreeMap 按 conditions[0][1] 排序（从小到大）
        TreeMap<Integer, ConfigAwardVo> sortedMap = new TreeMap<>();
        for (ConfigAwardVo awardConfig : data) {
            Integer[][] conditions = awardConfig.getGetCondition();

            // 校验 conditions 结构有效性
            if (conditions != null && conditions.length > 0 && conditions[0] != null && conditions[0].length > 1 && conditions[0][1] != null) {
                int sortKey = conditions[0][1]; // 提取排序值
                // 若 sortKey 重复，保留原对象或覆盖（根据需求选择）
                sortedMap.put(sortKey, awardConfig);
            }
        }
        // 将排序后的值转换为 LinkedList（已按 sortKey 升序排列）
        LinkedList<ConfigAwardVo> linkedList = new LinkedList<>(sortedMap.values());
        return linkedList;
    }


    public List<ConfigAwardVo> getAwardPoolById(Integer poolId) {
        return this.awardPoolMap.get(poolId);
    }


}
